//
//  RegisterView.m
//  乐清
//
//  Created by mac on 16/5/5.
//  Copyright © 2016年 Simon. All rights reserved.
//

#import "RegisterView.h"
#import "RegisterTableView.h"

@implementation RegisterView

- (void)dealloc {
    
    NSLog(@"%@被销毁了", self);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _pageNum = 0;
        [self _createTableView];
    }
    return self;
}

- (void)setViewType:(NSInteger)viewType {
    
    _viewType = viewType;
    
    _registerTableView.tableViewType = _viewType;
    
    [self _createHeaderWithPages:_pageNum];
}

- (void)_createHeaderWithPages:(int)page {
    
    for (UIView *sub in _headView.subviews) {
        [sub removeFromSuperview];
    }
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeaderHeight)];
        _headView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_headView];
    }
    
    CGFloat textFont = (kScreenWidth - kSpaceToSideCustom) / 21;
    NSArray *headTextArr = @[@"学生基本信息",@"第一监护人",@"第二监护人"];
    
    CGFloat usedWidth = 0;
    NSArray *wordCountArr = @[@6,@5,@5];
    for (int i = 0; i < headTextArr.count; i++) {
        NSInteger wordCount = [wordCountArr[i] integerValue];
        
        UIView *markView = [[UIView alloc] initWithFrame:CGRectMake(kSpaceToSideCustom / 2 + usedWidth, 0, (wordCount + 2) * textFont, kHeaderHeight)];
        if (i == 2) {
            markView.frame = CGRectMake(kSpaceToSideCustom / 2 + usedWidth, 0, (wordCount + 1) * textFont, kHeaderHeight);
        }
        [_headView addSubview:markView];
        usedWidth += markView.width;
        
        UILabel *markLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (kHeaderHeight - textFont + 2) / 2, textFont - 2, textFont - 2)];
        markLabel.backgroundColor = [UIColor grayColor];
        markLabel.text = [NSString stringWithFormat:@"%d", i + 1];
        markLabel.font = [UIFont systemFontOfSize:textFont - 2];
        markLabel.textColor = [UIColor whiteColor];
        markLabel.textAlignment = NSTextAlignmentCenter;
        if (i <= page) {
            markLabel.backgroundColor = [UIColor greenColor];
        }
        [markView addSubview:markLabel];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textFont, (kHeaderHeight - textFont) / 2, wordCount * textFont, textFont)];
        textLabel.backgroundColor = [UIColor whiteColor];
        textLabel.text = headTextArr[i];
        textLabel.textColor = [UIColor grayColor];
        textLabel.font = [UIFont systemFontOfSize:textFont];
        if (i <= page) {
            textLabel.textColor = [UIColor greenColor];
        }
        [markView addSubview:textLabel];
        
        if (i == 0) {
            _firstNextView = [[UIImageView alloc] initWithFrame:CGRectMake(textFont * (wordCount + 1) + 2, (kHeaderHeight - textFont) / 2 + 2, textFont - 4, textFont - 4)];
            
            _firstNextView.image = [UIImage imageNamed:@"process_gray@2x"];
            if (i < _pageNum) {
                _firstNextView.image = [UIImage imageNamed:@"process_green@2x"];
            }
//            _firstNextView.tag = 400 + i + _viewType * 10;
            [markView addSubview:_firstNextView];
        }
        if (i == 1) {
            _secondNextView = [[UIImageView alloc] initWithFrame:CGRectMake(textFont * (wordCount + 1) + 2, (kHeaderHeight - textFont) / 2 + 2, textFont - 4, textFont - 4)];
            
            _secondNextView.image = [UIImage imageNamed:@"process_gray@2x"];
            if (i < _pageNum) {
                _secondNextView.image = [UIImage imageNamed:@"process_green@2x"];
            }
//            _secondNextView.tag = 400 + i + _viewType * 10;
            [markView addSubview:_secondNextView];
        }
    }
}

- (void)_createTableView {
    
    if (!_registerTableView) {
        
        _registerTableView = [[RegisterTableView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, kScreenHeight)];
        
        [self addSubview:_registerTableView];
    }
    
    __weak RegisterView *weakSelf = self;
    _registerTableView.scrollToBottomBlock = ^(int isTrue) {
        if (weakSelf.pageNum == 0) {
            weakSelf.firstNextView.image = [UIImage imageNamed:@"process_green@2x"];
        } else if (weakSelf.pageNum == 1) {
            weakSelf.secondNextView.image = [UIImage imageNamed:@"process_green@2x"];
        }
    };
    
    _registerTableView.buttonIsShownBlock = ^(int isTrue) {
        if (weakSelf.pageNum == 0) {
            weakSelf.firstNextView.image = [UIImage imageNamed:@"process_green@2x"];
        } else if (weakSelf.pageNum == 1) {
            weakSelf.secondNextView.image = [UIImage imageNamed:@"process_green@2x"];
        }
    };

    _registerTableView.pageChangedBlock = ^(int page) {
        weakSelf.pageNum = page;
        [weakSelf _createHeaderWithPages:page];
    };
}


@end
